package com.motu.monstercity.server.game.logichandler;

import com.motu.monstercity.module.common.network.RequestParam;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.CsGameEvent.*;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.event.EventMain;
import com.motu.monstercity.server.game.commondata.fight.Stage;
import com.motu.monstercity.server.game.logic.EventManager;
import com.motu.monstercity.server.game.logic.FightManager;
import com.motu.monstercity.server.game.logic.GameUser;
import com.motu.monstercity.server.game.userdata.UserEvent;
import com.motu.monstercity.server.game.userdata.UserInfo;
import com.motu.monstercity.server.game.userdata.UserOnlineEvent;
import com.motu.monstercity.server.game.userdata.UserStage;
import com.motu.vertx.module.utility.base.ServerConfig;
import com.motu.vertx.module.utility.toolset.ErrorWord;

import java.util.List;

public class EventHandler {

    // CMD_GAME_EVENT_ONLINE_CREATE = 80101; // 请求信息
    public static Object CMD_GAME_EVENT_ONLINE_CREATE(RequestParam clientParam) throws Exception {
        long userId = clientParam.getUserId();
        CSGameEventOnlineCreateResponse.Builder response = CSGameEventOnlineCreateResponse.newBuilder();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        UserInfo userInfo = GameUser.getUserInfo(userId);
        EventManager.randomOnlineEvent(userInfo, pbUserData);
        return response;
    }
    // CMD_GAME_EVENT_INFO = 80104; // 请求信息
    public static Object CMD_GAME_EVENT_INFO(RequestParam clientParam) throws Exception {
        long userId = clientParam.getUserId();
        List<UserOnlineEvent> onlineEventList = EventManager.getUserOnlineEventList(userId);
        List<UserEvent> userEventList = EventManager.getUserEventList(userId);
        CSGameEventInfoResponse.Builder response = CSGameEventInfoResponse.newBuilder();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        for (UserOnlineEvent userOnlineEvent : onlineEventList) {
            ProtoDataUtils.updatePBUserData(pbUserData, userOnlineEvent, true);
        }
        for (UserEvent userEvent : userEventList) {
            ProtoDataUtils.updatePBUserData(pbUserData, userEvent, true);
        }
        return response;
    }

    // CMD_GAME_EVENT_COMPLETE_ONLINE = 80102; // 完成在线事件
    public static Object CMD_GAME_EVENT_COMPLETE_ONLINE(RequestParam clientParam) throws Exception {
        long userId = clientParam.getUserId();
        UserInfo userInfo = GameUser.getUserInfo(userId);
        CSGameEventCompleteOnlineRequest request = CSGameEventCompleteOnlineRequest.parseFrom(clientParam.getData());
        long dbid = request.getDbid();
        CSGameEventInfoResponse.Builder response = CSGameEventInfoResponse.newBuilder();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        UserOnlineEvent userOnlineEvent = EventManager.getUserOnlineEvent(userId, dbid);
        if (userOnlineEvent == null) {
            return ErrorWord.NOT_DATA;
        }

        List<Integer> param = request.getParamList();
        EventMain eventMain = EventManager.getEventMain(userOnlineEvent.getEventId());
        if (eventMain == null) {
            return ErrorWord.NOT_DATA_1;
        }
        if (eventMain.getEventType() == EventMain.EVENT_TYPE_NORMAL) {
            Object result = EventManager.recvOnlineEvent(userInfo, eventMain.getId(), pbUserData, 0);
            if (result != null) {
                return result;
            }
        } else if (eventMain.getEventType() == EventMain.EVENT_TYPE_CHOOSE) {
            if (param.isEmpty()) {
                return ErrorWord.BAD_PARAM;
            }
            int chooseItem = param.get(0);
            Object result = EventManager.recvOnlineEvent(userInfo, eventMain.getId(), pbUserData, chooseItem);
            if (result != null) {
                return result;
            }
        } else if (eventMain.getEventType() == EventMain.EVENT_TYPE_DISPATCH) {
            int matchCount = EventManager.getMatchParams(eventMain, param, userId);
            Object result = EventManager.recvOnlineEvent(userInfo, eventMain.getId(), pbUserData, matchCount);
            if (result != null) {
                return result;
            }
        }
        userOnlineEvent.delete();
        ProtoDataUtils.deletePBUserData(pbUserData, userOnlineEvent);
        EventManager.refreshEventCount(userId);
        return response;
    }

    // CMD_GAME_EVENT_COMPLETE_NORMAL = 80103; // 完成普通事件
    public static Object CMD_GAME_EVENT_COMPLETE_NORMAL(RequestParam clientParam) throws Exception {
        long userId = clientParam.getUserId();
        UserInfo userInfo = GameUser.getUserInfo(userId);
        CSGameEventCompleteNormalRequest request = CSGameEventCompleteNormalRequest.parseFrom(clientParam.getData());
        long dbid = request.getDbid();
        CSGameEventCompleteNormalResponse.Builder response = CSGameEventCompleteNormalResponse.newBuilder();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        UserEvent userEvent = EventManager.getUserEvent(userId, dbid);
        if (userEvent == null) {
            return ErrorWord.NOT_DATA;
        }
        List<Integer> param = request.getParamList();
        EventMain eventMain = EventManager.getEventMain(userEvent.getEventId());
        if (eventMain == null) {
            return ErrorWord.NOT_DATA_1;
        }
        if (eventMain.getEventType() == EventMain.EVENT_TYPE_NORMAL) {
            Object result = EventManager.recvOnlineEvent(userInfo, eventMain.getId(), pbUserData, 0);
            if (result != null) {
                return result;
            }
        } else if (eventMain.getEventType() == EventMain.EVENT_TYPE_CHOOSE) {
            if (param.isEmpty()) {
                return ErrorWord.BAD_PARAM;
            }
            int chooseItem = param.get(0);
            Object result = EventManager.recvOnlineEvent(userInfo, eventMain.getId(), pbUserData, chooseItem);
            if (result != null) {
                return result;
            }
        } else if (eventMain.getEventType() == EventMain.EVENT_TYPE_DISPATCH) {
            int matchCount = EventManager.getMatchParams(eventMain, param, userId);
            Object result = EventManager.recvOnlineEvent(userInfo, eventMain.getId(), pbUserData, matchCount);
            if (result != null) {
                return result;
            }
        }
        userEvent.delete();
        ProtoDataUtils.deletePBUserData(pbUserData, userEvent);
        return response;
    }

    // CMD_GAME_EVENT_NORMAL_CREATE = 80105; // 生成普通事件
    public static Object CMD_GAME_EVENT_NORMAL_CREATE(RequestParam clientParam) throws Exception {
        if (!ServerConfig.isDebugServer()) {
            return ErrorWord.INVALID_OPERATION;
        }
        CSGameEventNormalCreateRequest request = CSGameEventNormalCreateRequest.parseFrom(clientParam.getData());
        int eventId = request.getEventId();
        long userId = clientParam.getUserId();
        CSGameEventNormalCreateResponse.Builder response = CSGameEventNormalCreateResponse.newBuilder();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        UserInfo userInfo = GameUser.getUserInfo(userId);
//        if (eventId == 0) {
//            UserStage userStage = FightManager.getUserStage(userId);
//            int mapId = userStage.getMapId();
//            int stageId = userStage.getStageId();
//            Stage stage = FightManager.getStage(mapId, stageId);
//            if (null == stage) {
//                return ErrorWord.NOT_DATA_1;
//            }
//            EventManager.triggerNormalEvent(userInfo, stage, pbUserData);
//        } else {
//        }
        EventMain eventMain = EventManager.getEventMain(eventId);
        if (eventMain == null) {
            return ErrorWord.NOT_DATA;
        }
        UserEvent userEvent = new UserEvent(userId, eventMain.getId());
        userEvent.doCreate();
        ProtoDataUtils.updatePBUserData(pbUserData, userEvent, true);
        return response;
    }

}
